home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / Tetris Light 1.0 / source / menu_da.c < prev    next >
Text File  |  1993-08-05  |  6KB  |  244 lines

  1. /**********************************************************************\
  2.  
  3. File:        menu_da.c
  4.  
  5. Purpose:    This module handles the menus and desk accessories for the
  6.             ``Tetris Light'' program.
  7.             
  8.  
  9. ``Tetris Light'' - a simple implementation of a Tetris game.
  10. Copyright (C) 1993 Hoylen Sue
  11.  
  12. This program is free software; you can redistribute it and/or modify
  13. it under the terms of the GNU General Public License as published by
  14. the Free Software Foundation; either version 2 of the License, or
  15. (at your option) any later version.
  16.  
  17. This program is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. GNU General Public License for more details.
  21.  
  22. You should have received a copy of the GNU General Public License
  23. along with this program; see the file COPYING.  If not, write to the
  24. Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  25.  
  26. \**********************************************************************/
  27.  
  28. #include "local.h"
  29.  
  30. #include "about.h"
  31. #include "controls.h"
  32. #include "game.h"
  33. #include "highscore.h"
  34. #include "menu_da.h"
  35. #include "resources.h"
  36.  
  37. /*--------------------------------------------------------------------*/
  38.  
  39. /* Local globals */
  40.  
  41. static MenuHandle    apple_menu;
  42. static MenuHandle    file_menu;
  43. static MenuHandle    edit_menu;
  44. static MenuHandle    options_menu;
  45.  
  46. /*--------------------------------------------------------------------*/
  47.  
  48. Boolean menu_init(void)
  49. /* Sets up the menus for the application.  This routine is called at
  50.    the very beginning of the program.  It loads up the menus, and sets
  51.    them up for use.  If it cannot load the menu bar or menus, it will
  52.    return TRUE, FALSE is returned on success. */
  53. {
  54.     Handle    menu_bar;
  55.  
  56.     /* Load in the menus from the resources */
  57.         
  58.     menu_bar = GetNewMBar(MBAR_ID);
  59.     if (menu_bar == 0)
  60.         return TRUE; /* Failed */
  61.         
  62.     SetMenuBar(menu_bar);
  63.     DrawMenuBar ();
  64.  
  65.     apple_menu = GetMHandle(APPLE_MENU_ID);
  66.     file_menu = GetMHandle(FILE_MENU_ID);        
  67.     edit_menu = GetMHandle(EDIT_MENU_ID);
  68.     options_menu = GetMHandle(OPTIONS_MENU_ID);
  69.  
  70.     if (!apple_menu || !file_menu || !edit_menu || !options_menu)
  71.         return TRUE; /* Failed */
  72.  
  73.     /* Add entries for desk accessories */
  74.     
  75.     AddResMenu(apple_menu, 'DRVR');
  76.     
  77.     return FALSE; /* Success */
  78. }
  79.  
  80. /*--------------------------------------------------------------------*/
  81.  
  82. static void handle_menu_apple(INTEGER item)
  83. /* Processes selection of an item from the Apple menu. */
  84. {
  85.     Str255    da_name;
  86.     
  87.     switch (item) {
  88.     case APPLE_MITEM_ABOUT:
  89.         about_box();
  90.         break;
  91.     case 2:
  92.         /* Separator line, should not be selected */
  93.         SysBeep(5);
  94.         break;
  95.     default:
  96.         GetItem(apple_menu, item, da_name);
  97.         OpenDeskAcc(da_name);
  98.         break;
  99.     }
  100. }
  101.  
  102. /*--------------------------------------------------------------------*/
  103.  
  104. static Boolean handle_menu_file(INTEGER item)
  105. /* Processes selection of an item from the File menu.  Returns TRUE if
  106.    the ``Quit'' item was chosen, FALSE otherwise. */
  107. {    
  108.     switch ( item ) {
  109.     case FILE_MITEM_NEW:
  110.         game_new();
  111.         break;
  112.     case FILE_MITEM_OPEN:
  113.         game_open();
  114.         break;
  115.     case FILE_MITEM_SAVE:
  116.         game_save();
  117.         break;
  118.     case FILE_MITEM_SAVE_AS:
  119.         game_save_as();
  120.         break;
  121.     case FILE_MITEM_QUIT:
  122.         return TRUE;
  123.         break;
  124.     default:
  125.         /* Unknown item */
  126.         SysBeep(5);
  127.         break;
  128.     }
  129.     
  130.     return FALSE;
  131. }
  132.  
  133. /*--------------------------------------------------------------------*/
  134.  
  135. static void handle_menu_options(INTEGER item)
  136. /* Processes selection of an item from the Options menu. */
  137. {    
  138.     switch (item) {
  139.     case OPTIONS_MITEM_SHOWHIGH:
  140.         highscore_start();
  141.         break;
  142.     case OPTIONS_MITEM_SOUND:
  143.         ctrls.sound_on = !ctrls.sound_on;
  144.         break;
  145.     case OPTIONS_MITEM_NEXTPIECE:
  146.         ctrls.show_next_piece = !ctrls.show_next_piece;
  147.         break;
  148.     default:
  149.         /* Unknown item */
  150.         SysBeep(5);
  151.         break;
  152.     }
  153. }
  154.  
  155. /*--------------------------------------------------------------------*/
  156.  
  157. Boolean menu_choice(LONGINT choice)
  158. /* This routine is used to process a selection of a menu item. Returns
  159.    TRUE if the Quit option was chosen. */
  160. {
  161.     register Boolean quit_chosen = FALSE;
  162.     
  163.     if (choice) {
  164.         register INTEGER item = LoWord(choice);
  165.         
  166.         switch (HiWord(choice)) {
  167.         case APPLE_MENU_ID:
  168.             handle_menu_apple(item);
  169.             break;
  170.         case FILE_MENU_ID:
  171.             quit_chosen = handle_menu_file(item);
  172.             break;
  173.         case EDIT_MENU_ID:
  174.             if (item == EDIT_MITEM_KEYS)
  175.                 controls_edit();
  176.             else
  177.                 SystemEdit(item - 1);
  178.             break;
  179.         case OPTIONS_MENU_ID:
  180.             handle_menu_options(item);
  181.             break;
  182.         default:
  183.             /* Unknown menu */
  184.             SysBeep(5);
  185.             break;
  186.         }
  187.         HiliteMenu(0);
  188.     }
  189.     
  190.     return quit_chosen;
  191. }
  192.  
  193. /*--------------------------------------------------------------------*/
  194.  
  195. void adjust_menus()
  196. /* This routine activates or deactivates the "Edit" menu according to
  197.    whether the front window belongs to a DA or not. */
  198. {
  199.     if (is_DA_window(FrontWindow())) {
  200.         EnableItem(edit_menu, EDIT_MITEM_UNDO);
  201.         EnableItem(edit_menu, EDIT_MITEM_CUT);
  202.         EnableItem(edit_menu, EDIT_MITEM_COPY);
  203.         EnableItem(edit_menu, EDIT_MITEM_PASTE);
  204.         EnableItem(edit_menu, EDIT_MITEM_CLEAR);
  205.     }
  206.     else {
  207.         DisableItem(edit_menu, EDIT_MITEM_UNDO);
  208.         DisableItem(edit_menu, EDIT_MITEM_CUT);
  209.         DisableItem(edit_menu, EDIT_MITEM_COPY);
  210.         DisableItem(edit_menu, EDIT_MITEM_PASTE);
  211.         DisableItem(edit_menu, EDIT_MITEM_CLEAR);
  212.     }
  213.  
  214.     /* Set up menu to reflect internal settings */
  215.  
  216.     SetItemMark(options_menu, OPTIONS_MITEM_SOUND,
  217.                 ctrls.sound_on ? checkMark : noMark);
  218.     SetItemMark(options_menu, OPTIONS_MITEM_NEXTPIECE,
  219.                 ctrls.show_next_piece ? checkMark : noMark);
  220.                 
  221.     /* Don't allow user to save finished games */
  222.     
  223.     if (game_is_over()) {
  224.         DisableItem(file_menu, FILE_MITEM_SAVE);
  225.         DisableItem(file_menu, FILE_MITEM_SAVE_AS);
  226.     } else {
  227.         EnableItem(file_menu, FILE_MITEM_SAVE);
  228.         EnableItem(file_menu, FILE_MITEM_SAVE_AS);
  229.     }
  230. }
  231.  
  232. /*--------------------------------------------------------------------*/
  233.  
  234. Boolean is_DA_window(WindowPtr w_ptr)
  235. /* Determines whether the given 'w_ptr' is a desk accessory window. */
  236. {    
  237.     if (w_ptr == 0)
  238.         return FALSE;
  239.     else    /* DA windows have negative windowKinds */
  240.         return (((WindowPeek) w_ptr) -> windowKind < 0);
  241. }
  242.  
  243. /*--------------------------------------------------------------------*/
  244.